import filmring.Movie
import filmring.Director
import filmring.Actor
import filmring.Role
import filmring.FRUser
import filmring.Rating

class BootStrap {

    def init = { servletContext ->
        if(Movie.list().size()<130){
        def movieFile = new File("db/data.txt")
        movieFile.splitEachLine("\t"){
            //println(it)
            println(Movie.list().size())
            Director director = null;
            if(Director.findByName(it[1])!=null){
                director=Director.findByName(it[1])
            } else{
                director=new Director(name:it[1])
                println("I made a new director")
            }
            director.save(flush:true)
            println("Saved director ${it[1]}")
            if(Movie.findAllByTitle(it[0])!=null){
                Movie movie = new Movie(title:it[0], director:director)
                movie.save(flush:true)
                for(int i=2; i<it.size(); i++){
                    Actor actor = Actor.findByName(it[i])
                    if (actor ==null){
                        actor=new Actor(name:it[i])
                    }
                    actor.save(flush:true)
                    Role role = new Role()
                    role.movie = movie
                    role.actor = actor
                    role.save(flush:true)
                }
            }


           /* def newMovie = new Movie()
            newMovie.title = it[0]
            Director newDir = new Director(name:it[1])
           // newDir.addToMovies(newMovie)
            newDir.save(FailOnError:true)
            newMovie.director = newDir
            for(int x = 2; x<it.size(); x++){
                Role newRole = new Role()
                newRole.Movie = newMovie
                Actor newAct = new Actor(name:it[x])
                newRole.Actor = newAct
                newAct.save(FailOnError:true)
                newMovie.addToRoles(newRole)
                newAct.addToRoles(newRole)
                newAct.save(FailOnError:true)
            }
            newMovie.save(FailOnError:true)
            //println(newMovie.title)*/

        }
         def m1 = Movie.findByTitle("The Social Network")
        m1.releaseDate = new Date("01/11/2011")
        m1.save()

        def m2 = Movie.findByTitle("The Green Hornet")
        m2.releaseDate = new Date("05/03/2011")
        m2.save()

        def m3 = Movie.findByTitle("The Mechanic")
        m3.releaseDate = new Date("05/17/2011")
        m3.save()

        def m4 = Movie.findByTitle("Blue Valentine")
        m4.releaseDate = new Date("05/10/2011")
        m4.save()

        def m5 = Movie.findByTitle("No Strings Attached")
        m5.releaseDate = new Date("05/10/2011")
        m5.save()

        def m6 = Movie.findByTitle("The Dilemma")
        m6.releaseDate = new Date("05/03/2011")
        m6.save()

        if(FRUser.list().size()==0){
            def imagefiles=[Inception:"/FilmRing/images/Inception.jpg",
        "Blue Valentine":"/FilmRing/images/Inception.jpg",
        "No Strings Attached":"/FilmRing/images/NoStringsAttached.jpg",
        "The Social Network":"/FilmRing/images/TheSocialNetwork.jpg",
        "The Big Lebowski":"/FilmRing/images/TheBigLebowski.jpg",
        "The Green Hornet":"/FilmRing/images/TheGreenHornet.jpg",
        "The Life Aquatic with Steve Zissou":"/FilmRing/images/TheLifeAquatic.jpg",
        "The Mechanic":"/FilmRing/images/TheMechanic.jpg",
        "Wall Street":"/FilmRing/images/WallStreet.jpg"]
            FRUser admin = new FRUser(username:"admin", password:"admin", email:"film-ring@googlecode.com")
            admin.save()
            for(Movie m:Movie.list()){
                Rating rating = new Rating(user:admin, movie:m, rating:3)
                if(imagefiles.containsKey(m.title)){
                    rating.rating=5;
                }
                m.save()
                rating.save()
            }
        }
        }
        def imagefiles=[Inception:"/FilmRing/images/Inception.jpg",
        "The Big Lebowski":"/FilmRing/images/TheBigLebowski.jpg",
        "The Life Aquatic with Steve Zissou":"/FilmRing/images/TheLifeAquatic.jpg",
        "Wall Street":"/FilmRing/images/WallStreet.jpg",
        "Se7en":"/FilmRing/images/Seven.jpg"]
        FRUser admin = FRUser.findByUsername("admin")
        for(Rating rating:admin.ratings){
            if (imagefiles.containsKey(rating.movie.title)){
                rating.rating=5
                rating.save()
            }
        }
        println(Movie.list().size())
        println(Actor.list().size())
        println(Director.list().size())
    }
    def destroy = {
    }

}